package com.itheima.reggie.controlle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 用户管理
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,
                             HttpServletRequest request) {
        //接收账号密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        //判断账号是否存在
        LambdaQueryWrapper<Employee> queryWrapper = Wrappers.lambdaQuery(Employee.class)
                .eq(Employee::getUsername, username);
        //查询数据库用户名得到employee对象
        Employee emp = employeeService.getOne(queryWrapper);
        //判断前端查询到的用户名是否存在
        if (emp == null) {
            return R.error("用户名不存在");
        }

        //判断密码是否正确
        //用户密码加密 对比数据库加密过的密码
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!StringUtils.equals(password, emp.getPassword())) {
            return R.error("密码错误");
        }

        //判断账号状态
        if (emp.getStatus() == 0) {
            return R.error("此账号被禁用");
        }

        //记录登录者id 存入session
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //删除session数据
        request.getSession().invalidate();
        return R.success("删除成功");
    }

    /**
     * 新增员工
     * @param employee
     * @param
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        //设置字段内容
        String id = "123456";
        employee.setPassword(DigestUtils.md5DigestAsHex(id.getBytes()));
        //调service新增到数据库
        employeeService.save(employee);
        //返回code
        return R.success("添加成功!");
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
        Page<Employee> employeePage = employeeService.selectPage(page, pageSize, name);
        return R.success(employeePage);
    }

    /**
     * 账号启用禁用
     * 前端两个参数 使用对象实体类接收
     */
    @PutMapping
    public  R update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id回显修改信息
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
